package com.hundsun.fcp.webplugin.util;

import java.io.File;
import java.io.IOException;

import com.hundsun.fcp.webplugin.WebPluginConfig;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.text.TextUtils;

public class SdCardUtil {
	public static final String AndroidAssetHtmlNotExist = "android_asset_html_not_exist";
	public static final String ProtocolAndroidAsset = "file:///android_asset/";
	public static final String ProtocolFile = "file://";
	
	public static final String imageDir = "img" ;
	
	public static boolean existBaseDir(Context pContext) {
		return !TextUtils.isEmpty(getBaseDir(pContext));
	}

	public static String getBaseDir(Context pContext) {
		String path = getExternalPath(pContext);

		if (!TextUtils.isEmpty(path)) {
			path = path + "fcp";
		}
		
		if(existDir(path)){
			new File(path).mkdirs() ;
		}
		
		if ((!TextUtils.isEmpty(path)) && (!path.endsWith(File.separator))) {
			path = path + File.separator;
		}
		return path;
	}
	
	/**
	 * 返回图片保存路径
	 * @param pContext
	 * @return
	 */
	public static String getImageDir(Context pContext){
		String _imageDir = getBaseDir(pContext) + imageDir ;
		if(existDir(_imageDir)){
			FileUtil.makeDirectories(_imageDir) ;
		}
		
		return _imageDir ;
		
	}
	

	private static String getExternalPath(Context pContext) {
		String externalPath = null;
		if (existSdCard()) {
			externalPath = Environment.getExternalStorageDirectory()
					.getAbsolutePath();
		} else {
			externalPath = pContext.getCacheDir().getAbsolutePath();
		}

		if (!TextUtils.isEmpty(externalPath)) {
			if (!externalPath.endsWith(File.separator)) {
				externalPath = externalPath + File.separator;
			}
		}

		return externalPath;
	}

	public static boolean existSdCard() {
		return Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
	}

	private static boolean existDir(String pDir) {
		if (TextUtils.isEmpty(pDir)) {
			return false;
		}
		File dirFile = new File(pDir);
		if (dirFile.exists() && dirFile.isDirectory()) {
			return true;
		}

		return false;
	}

	public static boolean existAssets(Context pContext) {
		try {
			AssetManager _AssetManager = pContext.getAssets();
			int length = _AssetManager.list(getFcpPageDir(pContext)).length;
			return length != 0;
		} catch (Exception localException) {
			return false;
		}
	}
	
	public static String getSdCardWebPluginPath(Context pContext){
		 return getBaseDir(pContext)+WebPluginConfig.getWebBundleLocation() + File.separator;
	}

	
	public static final String getFcpPageDir(Context pContext) {
		return WebPluginConfig.getWebBundleLocation() + File.separator + "html";
	}

	/**
	 * html 页面位置
	 * @param pContext
	 * @return
	 */
	public static String locateHtmlFilePath(Context pContext) {
		String baseDir = getBaseDir(pContext);

		if (existDir(baseDir)) {
			return ProtocolFile + baseDir + getFcpPageDir(pContext) + File.separator;
		}
		if (existAssets(pContext)) {
			return ProtocolAndroidAsset + getFcpPageDir(pContext)
					+ File.separator;
		}
		return AndroidAssetHtmlNotExist;
	}
}
